<?php
/*
 * Author:  Somnath Shinde- soma.shinde@gmail.com Mobile No:- 9423792564
 * 
 * Create Date: 
 * 
 * Version of MYSQL_to_PHP: 1.1
 * 
 * Ranzani( YZ'S );
 * 
 */
require_once 'Base/BasePatients.class.php';

Class Patients {

	public function  __construct() {
			if( true == isset( $_SERVER['connection'] ) ) {
				$this->connection = $_SERVER['connection'];
			}
	}

	
	/*******************************************************************************************
	******************************** Fetch Functions *******************************************
	*******************************************************************************************/

	public function fetchSql( $strSql ) {
		$result = $this->connection->RunQuery( $strSql );
		$arrobjPatients = array();
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objPatients = new BasePatients();
			$objPatients->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objPatients->setFirstName( ( true == isset( $row["first_name"] ) ) ? $row["first_name"] : 'NULL' );
			$objPatients->setMiddleName( ( true == isset( $row["middle_name"] ) ) ? $row["middle_name"] : 'NULL' );
			$objPatients->setLastName( ( true == isset( $row["last_name"] ) ) ? $row["last_name"] : 'NULL' );
			$objPatients->setMotherName( ( true == isset( $row["mother_name"] ) ) ? $row["mother_name"] : 'NULL' );
			$objPatients->setAge( ( true == isset( $row["age"] ) ) ? $row["age"] : 'NULL' );
			$objPatients->setGender( ( true == isset( $row["gender"] ) ) ? $row["gender"] : 'NULL' );
			$objPatients->setBirthDate( ( true == isset( $row["birth_date"] ) ) ? $row["birth_date"] : 'NULL' );
			$objPatients->setAddress( ( true == isset( $row["address"] ) ) ? $row["address"] : 'NULL' );
			$objPatients->setMaritalStatus( ( true == isset( $row["marital_status"] ) ) ? $row["marital_status"] : 'NULL' );
			$objPatients->setOccupaction( ( true == isset( $row["occupaction"] ) ) ? $row["occupaction"] : 'NULL' );
			$objPatients->setHomeNumber( ( true == isset( $row["home_number"] ) ) ? $row["home_number"] : 'NULL' );
			$objPatients->setOfficeNumber( ( true == isset( $row["office_number"] ) ) ? $row["office_number"] : 'NULL' );
			$objPatients->setMobileNumber( ( true == isset( $row["mobile_number"] ) ) ? $row["mobile_number"] : 'NULL' );
			$objPatients->setEmailAddress( ( true == isset( $row["email_address"] ) ) ? $row["email_address"] : 'NULL' );
			$objPatients->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objPatients->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objPatients->setPatientTypeId( ( true == isset( $row["patient_type_id"] ) ) ? $row["patient_type_id"] : 'NULL' );
			$objPatients->setPatientAreaId( ( true == isset( $row["patient_area_id"] ) ) ? $row["patient_area_id"] : 'NULL' );
			$objPatients->setPatientPhotoPath( ( true == isset( $row["patient_photo_path"] ) ) ? $row["patient_photo_path"] : 'NULL' );
			$objPatients->setPatientIdProofPath( ( true == isset( $row["patient_id_proof_path"] ) ) ? $row["patient_id_proof_path"] : 'NULL' );
			$objPatients->setPatientResidentIdProofPath( ( true == isset( $row["patient_resident_id_proof_path"] ) ) ? $row["patient_resident_id_proof_path"] : 'NULL' );
			$objPatients->setCreatedBy( ( true == isset( $row["created_by"] ) ) ? $row["created_by"] : 'NULL' );
			$objPatients->setCreatedOn( ( true == isset( $row["created_on"] ) ) ? $row["created_on"] : 'NULL' );
			$objPatients->setUpdatedBy( ( true == isset( $row["updated_by"] ) ) ? $row["updated_by"] : 'NULL' );
			$objPatients->setUpdatedOn( ( true == isset( $row["updated_on"] ) ) ? $row["updated_on"] : 'NULL' );
			array_push( $arrobjPatients, $objPatients );
		}
		return $arrobjPatients;
	}
	public function fetchAllPatients() {
		$result = $this->connection->RunQuery("SELECT * FROM PATIENTS");
		$arrobjPatients = array();
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objPatients = new BasePatients();
			$objPatients->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objPatients->setFirstName( ( true == isset( $row["first_name"] ) ) ? $row["first_name"] : 'NULL' );
			$objPatients->setMiddleName( ( true == isset( $row["middle_name"] ) ) ? $row["middle_name"] : 'NULL' );
			$objPatients->setLastName( ( true == isset( $row["last_name"] ) ) ? $row["last_name"] : 'NULL' );
			$objPatients->setMotherName( ( true == isset( $row["mother_name"] ) ) ? $row["mother_name"] : 'NULL' );
			$objPatients->setAge( ( true == isset( $row["age"] ) ) ? $row["age"] : 'NULL' );
			$objPatients->setGender( ( true == isset( $row["gender"] ) ) ? $row["gender"] : 'NULL' );
			$objPatients->setBirthDate( ( true == isset( $row["birth_date"] ) ) ? $row["birth_date"] : 'NULL' );
			$objPatients->setAddress( ( true == isset( $row["address"] ) ) ? $row["address"] : 'NULL' );
			$objPatients->setMaritalStatus( ( true == isset( $row["marital_status"] ) ) ? $row["marital_status"] : 'NULL' );
			$objPatients->setOccupaction( ( true == isset( $row["occupaction"] ) ) ? $row["occupaction"] : 'NULL' );
			$objPatients->setHomeNumber( ( true == isset( $row["home_number"] ) ) ? $row["home_number"] : 'NULL' );
			$objPatients->setOfficeNumber( ( true == isset( $row["office_number"] ) ) ? $row["office_number"] : 'NULL' );
			$objPatients->setMobileNumber( ( true == isset( $row["mobile_number"] ) ) ? $row["mobile_number"] : 'NULL' );
			$objPatients->setEmailAddress( ( true == isset( $row["email_address"] ) ) ? $row["email_address"] : 'NULL' );
			$objPatients->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objPatients->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objPatients->setPatientTypeId( ( true == isset( $row["patient_type_id"] ) ) ? $row["patient_type_id"] : 'NULL' );
			$objPatients->setPatientAreaId( ( true == isset( $row["patient_area_id"] ) ) ? $row["patient_area_id"] : 'NULL' );
			$objPatients->setPatientPhotoPath( ( true == isset( $row["patient_photo_path"] ) ) ? $row["patient_photo_path"] : 'NULL' );
			$objPatients->setPatientIdProofPath( ( true == isset( $row["patient_id_proof_path"] ) ) ? $row["patient_id_proof_path"] : 'NULL' );
			$objPatients->setPatientResidentIdProofPath( ( true == isset( $row["patient_resident_id_proof_path"] ) ) ? $row["patient_resident_id_proof_path"] : 'NULL' );
			$objPatients->setCreatedBy( ( true == isset( $row["created_by"] ) ) ? $row["created_by"] : 'NULL' );
			$objPatients->setCreatedOn( ( true == isset( $row["created_on"] ) ) ? $row["created_on"] : 'NULL' );
			$objPatients->setUpdatedBy( ( true == isset( $row["updated_by"] ) ) ? $row["updated_by"] : 'NULL' );
			$objPatients->setUpdatedOn( ( true == isset( $row["updated_on"] ) ) ? $row["updated_on"] : 'NULL' );
			array_push( $arrobjPatients, $objPatients);
		}
		return $arrobjPatients;
	}
	public function fetchAllPatientsByIds( $arrintIds ) {
		$result = $this->connection->RunQuery("SELECT * FROM PATIENTS WHERE id IN ( implode( "," $arrintIds ) " );
		$arrobjPatients = array();
		while( $row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objPatients->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objPatients->setFirstName( ( true == isset( $row["first_name"] ) ) ? $row["first_name"] : 'NULL' );
			$objPatients->setMiddleName( ( true == isset( $row["middle_name"] ) ) ? $row["middle_name"] : 'NULL' );
			$objPatients->setLastName( ( true == isset( $row["last_name"] ) ) ? $row["last_name"] : 'NULL' );
			$objPatients->setMotherName( ( true == isset( $row["mother_name"] ) ) ? $row["mother_name"] : 'NULL' );
			$objPatients->setAge( ( true == isset( $row["age"] ) ) ? $row["age"] : 'NULL' );
			$objPatients->setGender( ( true == isset( $row["gender"] ) ) ? $row["gender"] : 'NULL' );
			$objPatients->setBirthDate( ( true == isset( $row["birth_date"] ) ) ? $row["birth_date"] : 'NULL' );
			$objPatients->setAddress( ( true == isset( $row["address"] ) ) ? $row["address"] : 'NULL' );
			$objPatients->setMaritalStatus( ( true == isset( $row["marital_status"] ) ) ? $row["marital_status"] : 'NULL' );
			$objPatients->setOccupaction( ( true == isset( $row["occupaction"] ) ) ? $row["occupaction"] : 'NULL' );
			$objPatients->setHomeNumber( ( true == isset( $row["home_number"] ) ) ? $row["home_number"] : 'NULL' );
			$objPatients->setOfficeNumber( ( true == isset( $row["office_number"] ) ) ? $row["office_number"] : 'NULL' );
			$objPatients->setMobileNumber( ( true == isset( $row["mobile_number"] ) ) ? $row["mobile_number"] : 'NULL' );
			$objPatients->setEmailAddress( ( true == isset( $row["email_address"] ) ) ? $row["email_address"] : 'NULL' );
			$objPatients->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objPatients->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objPatients->setPatientTypeId( ( true == isset( $row["patient_type_id"] ) ) ? $row["patient_type_id"] : 'NULL' );
			$objPatients->setPatientAreaId( ( true == isset( $row["patient_area_id"] ) ) ? $row["patient_area_id"] : 'NULL' );
			$objPatients->setPatientPhotoPath( ( true == isset( $row["patient_photo_path"] ) ) ? $row["patient_photo_path"] : 'NULL' );
			$objPatients->setPatientIdProofPath( ( true == isset( $row["patient_id_proof_path"] ) ) ? $row["patient_id_proof_path"] : 'NULL' );
			$objPatients->setPatientResidentIdProofPath( ( true == isset( $row["patient_resident_id_proof_path"] ) ) ? $row["patient_resident_id_proof_path"] : 'NULL' );
			$objPatients->setCreatedBy( ( true == isset( $row["created_by"] ) ) ? $row["created_by"] : 'NULL' );
			$objPatients->setCreatedOn( ( true == isset( $row["created_on"] ) ) ? $row["created_on"] : 'NULL' );
			$objPatients->setUpdatedBy( ( true == isset( $row["updated_by"] ) ) ? $row["updated_by"] : 'NULL' );
			$objPatients->setUpdatedOn( ( true == isset( $row["updated_on"] ) ) ? $row["updated_on"] : 'NULL' );
			array_push( $arrobjPatients, $objPatients );
		}
		return $arrobjPatients;
	}

	public function fetchPatientsById( $intId ) {
		$result = $this->connection->RunQuery( "SELECT * FROM PATIENTS WHERE id = " . $intId );
			$objPatients = NULL;
		while($row = $result->fetch_array( MYSQLI_ASSOC ) ) {
			$objPatients = new BasePatients();
			$objPatients->setId( ( true == isset( $row["id"] ) ) ? $row["id"] : 'NULL' );
			$objPatients->setFirstName( ( true == isset( $row["first_name"] ) ) ? $row["first_name"] : 'NULL' );
			$objPatients->setMiddleName( ( true == isset( $row["middle_name"] ) ) ? $row["middle_name"] : 'NULL' );
			$objPatients->setLastName( ( true == isset( $row["last_name"] ) ) ? $row["last_name"] : 'NULL' );
			$objPatients->setMotherName( ( true == isset( $row["mother_name"] ) ) ? $row["mother_name"] : 'NULL' );
			$objPatients->setAge( ( true == isset( $row["age"] ) ) ? $row["age"] : 'NULL' );
			$objPatients->setGender( ( true == isset( $row["gender"] ) ) ? $row["gender"] : 'NULL' );
			$objPatients->setBirthDate( ( true == isset( $row["birth_date"] ) ) ? $row["birth_date"] : 'NULL' );
			$objPatients->setAddress( ( true == isset( $row["address"] ) ) ? $row["address"] : 'NULL' );
			$objPatients->setMaritalStatus( ( true == isset( $row["marital_status"] ) ) ? $row["marital_status"] : 'NULL' );
			$objPatients->setOccupaction( ( true == isset( $row["occupaction"] ) ) ? $row["occupaction"] : 'NULL' );
			$objPatients->setHomeNumber( ( true == isset( $row["home_number"] ) ) ? $row["home_number"] : 'NULL' );
			$objPatients->setOfficeNumber( ( true == isset( $row["office_number"] ) ) ? $row["office_number"] : 'NULL' );
			$objPatients->setMobileNumber( ( true == isset( $row["mobile_number"] ) ) ? $row["mobile_number"] : 'NULL' );
			$objPatients->setEmailAddress( ( true == isset( $row["email_address"] ) ) ? $row["email_address"] : 'NULL' );
			$objPatients->setPatientId( ( true == isset( $row["patient_id"] ) ) ? $row["patient_id"] : 'NULL' );
			$objPatients->setDoctorId( ( true == isset( $row["doctor_id"] ) ) ? $row["doctor_id"] : 'NULL' );
			$objPatients->setPatientTypeId( ( true == isset( $row["patient_type_id"] ) ) ? $row["patient_type_id"] : 'NULL' );
			$objPatients->setPatientAreaId( ( true == isset( $row["patient_area_id"] ) ) ? $row["patient_area_id"] : 'NULL' );
			$objPatients->setPatientPhotoPath( ( true == isset( $row["patient_photo_path"] ) ) ? $row["patient_photo_path"] : 'NULL' );
			$objPatients->setPatientIdProofPath( ( true == isset( $row["patient_id_proof_path"] ) ) ? $row["patient_id_proof_path"] : 'NULL' );
			$objPatients->setPatientResidentIdProofPath( ( true == isset( $row["patient_resident_id_proof_path"] ) ) ? $row["patient_resident_id_proof_path"] : 'NULL' );
			$objPatients->setCreatedBy( ( true == isset( $row["created_by"] ) ) ? $row["created_by"] : 'NULL' );
			$objPatients->setCreatedOn( ( true == isset( $row["created_on"] ) ) ? $row["created_on"] : 'NULL' );
			$objPatients->setUpdatedBy( ( true == isset( $row["updated_by"] ) ) ? $row["updated_by"] : 'NULL' );
			$objPatients->setUpdatedOn( ( true == isset( $row["updated_on"] ) ) ? $row["updated_on"] : 'NULL' );
		}
		return $objPatients;
	}

	public function GetKeysOrderBy($column, $order){
		$keys = array(); $i = 0;
		$result = $this->connection->RunQuery("SELECT id from patients order by $column $order");
			while($row = $result->fetch_array(MYSQLI_ASSOC)){
				$keys[$i] = $row["id"];
				$i++;
			}
	return $keys;
	}

	public function endPatients(){
		$this->connection->CloseMysql();
	}

}